var CusNameSpace = window.CusNameSpace || {};
/*
 Class UpdateTopState
 Function  Top Refrush
 */
CusNameSpace.CusProtocol = function () {
    this.name = 'CusProtocol';

};

CusNameSpace.CusProtocol.prototype.func_manual_move = function (mx,my,mr) {
    //websocket消息发送
    var cmdRowParent = {};
    var cmdRowChild = {};
    cmdRowChild.move_x = mx;
    cmdRowChild.move_y = my;
    cmdRowChild.move_rotate = mr;

    cmdRowParent.manual_move = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
    
    $("#id_test_output").text(sendData);
    
};
//呼叫车辆
CusNameSpace.CusProtocol.prototype.func_set_call_vehicle = function (inParameter) {
    //关机指令
    var cmdRowParent = {};
    var cmdRowChild = {};

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    
    
    cmdRowChild.target_position = inParameter.target_position;
    cmdRowChild.comment = inParameter.comment;
    cmdRowChild.user_ip = inParameter.user_ip;

    cmdRowParent.set_call_vehicle = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};
//手动按键控制停止
CusNameSpace.CusProtocol.prototype.func_manual_stop = function () {
    var cmdRowParent = {};
    var cmdRowChild = {};
    cmdRowChild.parameter = 0;
    cmdRowParent.manual_stop = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
    $("#id_test_output").text(sendData);
    
    return sendData;
};
CusNameSpace.CusProtocol.prototype.func_set_shutdown = function (inShutDownType) {
    //关机指令
    var cmdRowParent = {};
    var cmdRowChild = {};
    var shutDownType=parseInt(inShutDownType);
    
    if(shutDownType< enumTypeShutDownType.kShutDown || shutDownType> enumTypeShutDownType.kReboot)
    {
        shutDownType = enumTypeShutDownType.kShutDown;
    }
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.shutdown_type = shutDownType;

    cmdRowParent.set_shutdown = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};
CusNameSpace.CusProtocol.prototype.func_set_run = function () {
    var cmdRowParent = {};
    var cmdRowChild = {};

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.parameter = 0;

    cmdRowParent.set_run = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};
CusNameSpace.CusProtocol.prototype.func_set_mode = function (modeType) {
    var cmdRowParent = {};
    var cmdRowChild = {};
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.mode = modeType;
    cmdRowParent.set_mode = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);

    $(".group_top_pop_mode").css("background-color", "#337ab7");
};
CusNameSpace.CusProtocol.prototype.func_set_clear_odomter = function () {
    var cmdRowParent = {};
    var cmdRowChild = {};
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.parameter = 0;

    cmdRowParent.set_clear_odomter = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};
CusNameSpace.CusProtocol.prototype.func_set_save_map = function () {
    var cmdRowParent = {};
    var cmdRowChild = {};
    var mapName = "";

    mapName = $("#id_content_map_input_name").val();
    
    if(mapName.length <= 0)
    {
        popTipWindowMsg("地图名称不允许为空值！");
        return;
    }

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.map_name = mapName;

    cmdRowParent.set_save_map = cmdRowChild;
   
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};
CusNameSpace.CusProtocol.prototype.func_set_create_map_start = function () {
    //创建地图
    var cmdRowParent = {};
    var cmdRowChild = {};

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.parameter = 0;

    cmdRowParent.set_create_map_start = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};

CusNameSpace.CusProtocol.prototype.func_set_clear_msg = function () {
    var cmdRowParent = {};
    var cmdRowChild = {};
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.parameter = 0;

    cmdRowParent.set_clear_msg = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};
CusNameSpace.CusProtocol.prototype.func_get_robot_program_files_strings = function () {
    this.func_get_robot_program_files_strings_language(enumTypeProgramLanguageType.kLanuageEn);
    this.func_get_robot_program_files_strings_language(enumTypeProgramLanguageType.kLanuageCh);
};
CusNameSpace.CusProtocol.prototype.func_get_robot_program_files_strings_language = function (languageType) {

    var cmdRowParent = {};
    var cmdRowChild = {};
    //正在运行的程序
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.language_type = parseInt(languageType);

    cmdRowParent.get_robot_program_files_strings = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);

    window.objGlobalManage.objWebSocket.sendMessage(sendData);
    
    //显示文件列表-第一个页面
    //显示程序-查看-子窗口
    if(window.objGlobalManage.jumpProgramFileStringWindowOpen === 0)
    {
        window.objGlobalManage.objCusEventHandle.windowShowHideProgramView("page_process_m_position_filename");
    }
    return sendData;
};
CusNameSpace.CusProtocol.prototype.func_set_vmark_laser_filter_type = function () {

    var cmdRowParent = {};
    var cmdRowChild = {};
    var vmarkLaserValue = "";
    vmarkLaserValue = $("#id_content_set_vmark_laserfilter").val();
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.laser_filter_type = parseInt(vmarkLaserValue);
    cmdRowParent.set_vmark_laser_filter_type = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);

    return sendData;
};

CusNameSpace.CusProtocol.prototype.func_set_vmark_type = function () {

    var cmdRowParent = {};
    var cmdRowChild = {};
    var vmarkValue = "";
    vmarkValue = $("#id_content_set_vmark_type").val();

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    //cmdRowChild.program_file_name=fileDataInfo.fileName;
    //cmdRowChild.map_name=window.objGlobalManage.objCusUpdateTopState.variable_map_name;
    cmdRowChild.vmark_type = parseInt(vmarkValue);
    cmdRowParent.set_vmark_type = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);

    return sendData;
};

//移动到点
CusNameSpace.CusProtocol.prototype.func_move_to_navgation_point = function () {

    var cmdRowParent = {};
    var cmdRowChild = {};
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.dpoint_x = window.objGlobalManage.objCusProgramMode.read_dpoint_x;
    cmdRowChild.dpoint_y = window.objGlobalManage.objCusProgramMode.read_dpoint_y;
    cmdRowChild.dpoint_r = window.objGlobalManage.objCusProgramMode.read_dpoint_r;
    cmdRowChild.speed_ratio = window.objGlobalManage.objCusProgramMode.instruction_speedratio;
    cmdRowChild.positive_direction = window.objGlobalManage.objCusProgramMode.instruction_direction;
    cmdRowChild.dp_type = window.objGlobalManage.objCusProgramMode.instruction_dp_type;
    cmdRowChild.qrcode_type_num = window.objGlobalManage.objCusProgramMode.instruction_qrcodenumber;
    cmdRowChild.vmark_type_num = window.objGlobalManage.objCusProgramMode.instruction_vmarknumber;
    cmdRowChild.laser_type_num = window.objGlobalManage.objCusProgramMode.instruction_lasernumber;
    cmdRowChild.detect_barrier = window.objGlobalManage.objCusProgramMode.instruction_detect;
    cmdRowParent.move_to_navgation_point = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
    return sendData;
};

//获取VMARK调试信息
CusNameSpace.CusProtocol.prototype.func_get_vmark_debug_info = function () {

    var cmdRowParent = {};
    var cmdRowChild = {};

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.parameter = 0;
    cmdRowParent.get_vmark_debug_info = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);

    return sendData;
};

//设置默认文件名
CusNameSpace.CusProtocol.prototype.func_set_default_file_name = function (inParam) {
    var cmdRowParent = {};
    var cmdRowChild = {};
    if(inParam.length<=0)
    {
        return "";
    }
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.default_file_name = inParam;
    cmdRowParent.set_default_file_name = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
    return sendData;
};
//设置运行行号
CusNameSpace.CusProtocol.prototype.func_set_run_line_number = function (inParam) {
    var cmdRowParent = {};
    var cmdRowChild = {};
    if(parseInt(inParam)<0)
    {
        return "";
    }
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.line_number = parseInt(inParam);
    cmdRowParent.set_run_line_number = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
    return sendData;
};
//加载地图
CusNameSpace.CusProtocol.prototype.func_set_load_map = function (targetElement) {
    
    var itemIndex = targetElement.attr("item-index");
    if (itemIndex !== "" && itemIndex >= 0)
    {
        window.objGlobalManage.objCusUpdateTopState.selectMapNameIndex = itemIndex;
        var mapName = window.objGlobalManage.objCusUpdateTopState.arrayMapName[itemIndex];

        var cmdRowParent = {};
        var cmdRowChild = {};
        cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
        cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
        cmdRowChild.map_name = mapName;
        cmdRowParent.set_load_map = cmdRowChild;
        var sendData = JSON.stringify(cmdRowParent);
        window.objGlobalManage.objWebSocket.sendMessage(sendData);
    }
};
//设置DO
CusNameSpace.CusProtocol.prototype.func_set_do= function (targetElement) {
    
    var itemIndex = 0;
    itemIndex = targetElement.attr("item-index");
    if (itemIndex != "" && itemIndex >= 0)
    {

        window.objGlobalManage.objCusUpdateTopState.selectDoIndex = itemIndex;
        var doState = 0;
        if (window.objGlobalManage.objCusUpdateTopState.arrayDoState[itemIndex] == enumTypeDoState.kDoClose)
        {
            doState = enumTypeDoState.kDoOpen;
            //targetElement.text("开");
        } else
        {
            doState = enumTypeDoState.kDoClose;
            //targetElement.text("关");
        }

        var cmdRowParent = {};
        var cmdRowChild = {};
        cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
        cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
        cmdRowChild.index = Number(itemIndex);
        cmdRowChild.value = doState;

        cmdRowParent.set_do = cmdRowChild;
        var sendData = JSON.stringify(cmdRowParent);
        window.objGlobalManage.objWebSocket.sendMessage(sendData);
    }
};
//设置默认地图
CusNameSpace.CusProtocol.prototype.func_set_default_map_name= function (targetElement) {
    
    var itemIndex = targetElement.attr("item-index");
    if (itemIndex !== "" && itemIndex >= 0)
    {
        window.objGlobalManage.objCusUpdateTopState.selectMapNameIndex = itemIndex;
        var mapName = window.objGlobalManage.objCusUpdateTopState.arrayMapName[itemIndex];

        var cmdRowParent = {};
        var cmdRowChild = {};
        cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
        cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
        cmdRowChild.map_name = mapName;
        cmdRowParent.set_default_map_name = cmdRowChild;
        var sendData = JSON.stringify(cmdRowParent);
        window.objGlobalManage.objWebSocket.sendMessage(sendData);
    }
};
CusNameSpace.CusProtocol.prototype.func_set_cease= function () {
    var cmdRowParent = {};
    var cmdRowChild = {};
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.parameter = 0;

    cmdRowParent.set_cease = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};

CusNameSpace.CusProtocol.prototype.func_set_stop= function () {

    var cmdRowParent = {};
    var cmdRowChild = {};

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.parameter = 0;

    cmdRowParent.set_stop = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};
CusNameSpace.CusProtocol.prototype.func_set_current_running_program_file= function () {
    var itemIndex = window.objGlobalManage.objCusUpdateTopState.selectProgramOpenIndex;
    var programName = window.objGlobalManage.objCusUpdateTopState.arrayProgramOpen[itemIndex];
    var cmdRowParent = {};
    var cmdRowChild = {};

    window.objGlobalManage.objCusProgramMode.jumpProgramWindowOpenToView = programName;

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.file_name = programName;

    cmdRowParent.set_current_running_program_file = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};

CusNameSpace.CusProtocol.prototype.func_set_current_running_program_file_in_fileline= function () {
    var cmdRowParent = {};
    var cmdRowChild = {};
    var fileNameCount = window.objGlobalManage.objCusUpdateTopState.arrayProgramPosition.length;
    if (window.objGlobalManage.objCusUpdateTopState.selectProgramPositionIndex < fileNameCount)
    {
        var nameIndex = window.objGlobalManage.objCusUpdateTopState.selectProgramPositionIndex;
        var itemData = window.objGlobalManage.objCusUpdateTopState.arrayProgramPosition[nameIndex];

        cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
        cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
        cmdRowChild.file_name = itemData.fileName;

        cmdRowParent.set_current_running_program_file = cmdRowChild;
        var sendData = JSON.stringify(cmdRowParent);
        window.objGlobalManage.objWebSocket.sendMessage(sendData);

        //加载文件列表
        window.objGlobalManage.objCusUpdateTopState.updateProgramFileLine(window.objGlobalManage.objCusUpdateTopState.selectProgramPositionIndex);

    }
};
CusNameSpace.CusProtocol.prototype.func_set_servo= function (inType) {
    var cmdRowParent = {};
    var cmdRowChild = {};
    
    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.ison = parseInt(inType);

    cmdRowParent.set_servo = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};

//登录
CusNameSpace.CusProtocol.prototype.func_set_login= function () {
    
    var inputPassWd = "";
    //获取密码
    inputPassWd = $("#modle_in_login_pass").val();
    do {
        if (window.objGlobalManage.objCusUpdateTopState.variable_state_user === -1)
        {
            //提示选择用户
            popTipWindowMsg("请选择用户\n");
            break;
        }

        if (inputPassWd.length <= 0)
        {
            //提示输入密码
            popTipWindowMsg("请输入密码\n");
            break;

        }
        var cmdRowParent = {};
        var cmdRowChild = {};

        cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
        cmdRowChild.user_password = inputPassWd;
        cmdRowChild.parameter = 0;

        cmdRowParent.set_login = cmdRowChild;
        var sendData = JSON.stringify(cmdRowParent);
        window.objGlobalManage.objWebSocket.sendMessage(sendData);
        

        //记录登录密码
        window.objGlobalManage.objCusUpdateTopState.loginPassWd = inputPassWd;

        //顶部显示用户角色
        if (window.objGlobalManage.objCusUpdateTopState.variable_state_user == enumTypeUser.kUserManage)
        {
            $("#id_top_pop_user").text("管理员");
        } else
        {
            $("#id_top_pop_user").text("操作员");
        }
    } while (0);
};
CusNameSpace.CusProtocol.prototype.func_set_ultrasonic_sensor_check_enable= function () {
    //超声波检测设置
    var cmdRowParent = {};
    var cmdRowChild = {};

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;
    cmdRowChild.check_enable = window.objGlobalManage.objCusUpdateTopState.objAvoidInfo.ultrasonicSensorEnable;

    if (window.objGlobalManage.objCusUpdateTopState.objAvoidInfo.ultrasonicSensorEnable === enumTypeUltrasonicSensorEnable.kUltrasonicSensorOpen)
    {
        cmdRowChild.check_enable = enumTypeUltrasonicSensorEnable.kUltrasonicSensorClose;
    } else
    {
        cmdRowChild.check_enable = enumTypeUltrasonicSensorEnable.kUltrasonicSensorOpen;
    }

    cmdRowParent.set_ultrasonic_sensor_check_enable = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};
CusNameSpace.CusProtocol.prototype.func_set_point_cloud_check_enable= function () {
    //点云检测设置
    var cmdRowParent = {};
    var cmdRowChild = {};

    cmdRowChild.user_type = window.objGlobalManage.objCusUpdateTopState.variable_state_user;
    cmdRowChild.user_password = window.objGlobalManage.objCusUpdateTopState.loginPassWd;

    if (window.objGlobalManage.objCusUpdateTopState.objAvoidInfo.pointCloudEnable === enumTypePointCloudEnable.kPointCloudOpen)
    {
        cmdRowChild.check_enable = enumTypePointCloudEnable.kPointCloudClose;
    } else
    {
        cmdRowChild.check_enable = enumTypePointCloudEnable.kPointCloudOpen;
    }

    cmdRowParent.set_point_cloud_check_enable = cmdRowChild;
    var sendData = JSON.stringify(cmdRowParent);
    window.objGlobalManage.objWebSocket.sendMessage(sendData);
};

 